﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using Entities;
using RepositoriesInterfaces;
using NHibernate.Linq;

namespace Repositories
{
    public class LoadAtomRepository : BaseRepository<LoadAtom>, ILoadAtomRepository
    {
        public LoadAtomRepository(ISession session)
        {
            this._session = session;
        }

        public IList<LoadAtom> GetDistributionLoad(int DepartmentId, int TeacherId)
        {
            _session.BeginTransaction();
            IList<LoadAtom> distributionLoad = (from s in _session.Query<LoadAtom>()
                                                from x in _session.Query<Allocation>()
                                                from y in x.StudyPlanItems
                                                where y.Department.Id == DepartmentId
                                                where x.Id == s.Allocation.Id
                                                where s.Teacher.Id == TeacherId
                                                select s).Distinct().ToList();
            _session.Transaction.Commit();
            return distributionLoad;
        }

        public IList<LoadAtom> GetNotDistributionLoad(int DepartmentId)
        {
            _session.BeginTransaction();
            IList<LoadAtom> distributionLoad = (from s in _session.Query<LoadAtom>()
                                                from x in _session.Query<Allocation>()
                                                from y in x.StudyPlanItems
                                                where y.Department.Id == DepartmentId
                                                where x.Id == s.Allocation.Id
                                                where s.Teacher == null
                                                select s).Distinct().ToList();
            _session.Transaction.Commit();
            return distributionLoad;
        }


        public IList<LoadAtom> GetByDisciplineAndType(int departmentId, int disciplineId, string loadType, Teacher teacher)
        {
            _session.BeginTransaction();
            IList<LoadAtom> distributionLoad = (from s in _session.Query<LoadAtom>()
                                                from x in _session.Query<Allocation>()
                                                from y in x.StudyPlanItems
                                                where y.Discipline.Id == disciplineId
                                                where y.Department.Id == departmentId
                                                where x.Id == s.Allocation.Id
                                                where s.Teacher == teacher
                                                where s.LoadType == loadType
                                                select s).Distinct().ToList();
            _session.Transaction.Commit();
            return distributionLoad;
        }


        public IList<LoadAtom> GetByDiscipline(int departmentId, int disciplineId, Teacher teacher)
        {
            _session.BeginTransaction();
            IList<LoadAtom> distributionLoad = (from s in _session.Query<LoadAtom>()
                                                from x in _session.Query<Allocation>()
                                                from y in x.StudyPlanItems
                                                where y.Discipline.Id == disciplineId
                                                where y.Department.Id == departmentId
                                                where x.Id == s.Allocation.Id
                                                where s.Teacher == teacher                                                
                                                select s).Distinct().ToList();
            _session.Transaction.Commit();
            return distributionLoad;
        }
    }
}